package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.comm.CNSpell;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class GoodsclassOS extends OperatingService{
	private static final Log logger = LogFactory.getLog(GoodsOS.class);
	
	public GoodsclassOS(Connection conn){
		super(conn);
	}
	
	public GoodsclassOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(GoodsclassTB goodsclass){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> insertMap = new HashMap<String, Object>();

		insertMap.put(GoodsclassTB.PARENTID_GOODSCLASS, goodsclass.getParentid_goodsclass());
		insertMap.put(GoodsclassTB.NAME_GOODSCLASS, goodsclass.getName_goodsclass());
		insertMap.put(GoodsclassTB.REMARK_GOODSCLASS, goodsclass.getRemark_goodsclass());
		
		insertMap.put(GoodsclassTB.CNSPELL_GOODSCLASS, CNSpell.CNToSpell(goodsclass.getName_goodsclass()));
		insertMap.put(GoodsclassTB.FWCNSPELL_GOODSCLASS, CNSpell.CNToFirstWordSpell(goodsclass.getName_goodsclass())); 
		
		return sqlMachine.insertRow(GoodsclassTB.TABLE_NAME, insertMap);
	}
	
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<GoodsclassTB> selectListBySql(String sql){
		List<GoodsclassTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<GoodsclassTB>();
					while(resultSet.next()){
						GoodsclassTB goodsclass = new GoodsclassTB();
						goodsclass.setCnspell_goodsclass(resultSet.getString(GoodsclassTB.CNSPELL_GOODSCLASS));
						goodsclass.setFwcnspell_goodsclass(resultSet.getString(GoodsclassTB.FWCNSPELL_GOODSCLASS));
						goodsclass.setId_goodsclass(resultSet.getInt(GoodsclassTB.ID_GOODSCLASS));
						goodsclass.setName_goodsclass(resultSet.getString(GoodsclassTB.NAME_GOODSCLASS));
						goodsclass.setParentid_goodsclass(resultSet.getInt(GoodsclassTB.PARENTID_GOODSCLASS));
						goodsclass.setRemark_goodsclass(resultSet.getString(GoodsclassTB.REMARK_GOODSCLASS));
					
				
						list.add(goodsclass);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	public List<GoodsclassTB> select(){
		
		List<GoodsclassTB> list = null;
		String sql = "SELECT * FROM " + GoodsclassTB.TABLE_NAME ; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	public GoodsclassTB getGoodsclassById(int id){
		
		String sql = "SELECT * FROM " + GoodsclassTB.TABLE_NAME + " where " + GoodsclassTB.ID_GOODSCLASS + "=" + id;
		GoodsclassTB goodsclass = new GoodsclassTB();
		List<GoodsclassTB> goodsclassList = selectListBySql(sql);
		goodsclass = goodsclassList.get(0);
		return goodsclass;
	}  
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxId(){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(GoodsclassTB.ID_GOODSCLASS, GoodsclassTB.TABLE_NAME);
		return maxId;
	}
	
	
	
	
	

}
